<?php
/**
 * Front Controller plug in to set up the view with the Places view helper
 * path and some useful request variables.
 *
 */
class Wmp_Controller_Plugin_CssSetup extends Zend_Controller_Plugin_Abstract
{    
    /**
     * @var Zend_View
     */
    protected $_view; 
    
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $this->_view = $viewRenderer->view;
    	
    	$config = Zend_Registry::get('config');
    	$view = $this->_view;
    	$controller = zend_registry::get('controller');
        $module = zend_registry::get('module');		
        $ModuleTemplate = zend_registry::get('template');          
    	
        $template_path = '/templates/'.$ModuleTemplate.'/css';
        $local_path = $template_path.'/'.$request->getParam('language');
        $module_path = $local_path . '/' . $module;        
		
		$cssList = array(
				        'template_css' => $template_path.'/template',		
						'local_css' => $local_path .'/local',
						'module_css' =>  $module_path.'/module',
						'module_controller_css' => $module_path.'/controller.'.$controller
					);
		$this->_addCss( $cssList );
		$this->_addCss( $cssList, "print" );	
    }
    
	private function _addCss($cssArray,$type="screen,projection")
    {
    	$config = zend_registry::get('config');
    	$extension =".css";
    	
    	if($type=="print") $extension=".print.css";  	
    	
    	foreach($cssArray as $key=>$val){
	    	$cssfile = $val.$extension;    		
	    	if (file_exists($config->path->public->dir.$cssfile)) {
	    		
	            $url = $this->_view->baseUrl().$cssfile;        
	            $this->_view->headLink()->appendStylesheet($url , $type);
	       	}
    	}    	
    }
}